﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CollaborateTools;
using System.Drawing;
using ATEC.ATDataCoreNET;
using SSDPNET;
using System.IO;
using ATEC.ATDataCoreNET.ATObjects;

namespace DoModelExample
{
    
    public class TreeManager
    {
        public static GlobalManagementForm gForm = GlobalManagementForm.Instance;
        public static TreeView m_treeView = gForm.CurrentTreeView;

        public static MenuItem m_openDrawingMenuItem = new MenuItem("打开图纸");//打开出图
        public static MenuItem m_Switch2DTo3D = new MenuItem("切换");//建模和出图变换
        public static TreeNode pRoot = null;//工程根节点
        //获取当前激活Scene
        public static ATSceneRef scene = ScenesFunctions.SceneRefGetActive();
        public static bool is3DProject = true;//当前是否是建模工程
        public TreeManager()
        {
            //初始化事件绑定
            InitCommand();
            InitMenuItem();
        }


        public static TreeManager Entity()
        {
            return new TreeManager();
        }

        public void CreateProjectTree()
        {
            string filePath = Path.GetDirectoryName(CreateNewProject.GetDBName());
            string curProjectName = Path.GetFileNameWithoutExtension(filePath);
            var info = DbGeneralInfo.Peek();
            if (info.OwnerName != "Net示例工程")
            {
                return;
            }


            //清空工程节点
            //没有-->创建
            //有-->清空
            gForm.ClearDbChildNodes(GNodeType.Tunnel, curProjectName);

            pRoot = gForm.GetDbNode(GNodeType.Tunnel, curProjectName);
            if (pRoot == null)
            {
                return; // 整体树出错，提前返回
            }
            RefreshTreeNode(ref pRoot);

            gForm.TryRefreshTreeView();
            gForm.ExpandNodeByGNodeType(GNodeType.Tunnel);
        }

        /// <summary>
        /// 构造节点
        /// </summary>
        /// <param name="treeView"></param>
        public void RefreshTreeNode(ref TreeNode pRoot)
        {
            if(CreateComponentExample.componentModel == null)
            {
                return;
            }
            string name = "构件" + "(" + CreateComponentExample.componentModel.component.startStation.ToString() 
                + "~" + CreateComponentExample.componentModel.component.endStation.ToString() + ")";        
            TreeNode newNode = new TreeNode(name);
            pRoot.Nodes.Add(newNode);
        }

        /// <summary>
        /// 初始化全局管理窗体事件
        /// </summary>
        public void InitCommand()
        {
            GlobalManagementForm.TreeNodeMouseClick += TreeViewAfterSelect;

        }
        private void UnInitCommand()
        {
            GlobalManagementForm.TreeNodeMouseClick -= TreeViewAfterSelect;
        }

        /// <summary>
        /// 初始化右键菜单
        /// </summary>
        public void InitMenuItem()
        {
            //menu
            m_openDrawingMenuItem.Click += OpenDrawingMenuItem_Click;
            m_Switch2DTo3D.Click += Switch2DTo3D;
        }
        private void UnInitMenuItem()
        {
            //menu
            m_openDrawingMenuItem.Click -= OpenDrawingMenuItem_Click;
            m_Switch2DTo3D.Click -= Switch2DTo3D;
        }

        public void Dispose()
        {
            UnInitCommand();
            UnInitMenuItem();
        }

        /// <summary>
        /// 右键选中节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewAfterSelect(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeView tv = (TreeView)(sender);
                if (tv != null)
                {
                    m_treeView = tv;
                    m_treeView.SelectedNode = e.Node;
                    AddMenu(e);
                }
            }
        }

        /// <summary>
        /// 添加右键菜单
        /// </summary>
        /// <param name="e"></param>
        public void AddMenu(TreeNodeMouseClickEventArgs e)
        {
            ContextMenu contextMenu = new ContextMenu();

            TreeNode nodeData = e.Node;
            if (nodeData == null)
                return;

            if(nodeData.Text.Contains("图纸"))
            {
                contextMenu.MenuItems.Add(m_openDrawingMenuItem);       
                contextMenu.MenuItems.Add(m_Switch2DTo3D);
            }

            contextMenu.Show(m_treeView, e.Location);
        }
        /// <summary>
        /// 打开出图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void OpenDrawingMenuItem_Click(object sender, EventArgs e)
        {          
        
            string filePath = Path.GetDirectoryName(CreateNewProject.GetDBName()) + "/出图.db";
            string fileName = Path.GetFileName(filePath);
            ATSceneRef defaultSceneRef = null;
            if (StatusInt.Success == ScenesFunctions.SceneRefGetDefaultSceneRef(out defaultSceneRef, ScenesFunctions.SceneRefGetActive(), false, false, false))
            {
                ATSystem.SystemNewDesignFile(fileName);
                CollaborateTools.ViewportModeManager.SwitchToDefaultDrawingMode();
                is3DProject = false;
                ViewManager.FitView();
            }

        }
        /// <summary>
        /// 出图切换到3D
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Switch2DTo3D(object sender, EventArgs e)
        {          
            if (!is3DProject)
            {
                //打开3D
                int nOpen =ATSystem.SystemNewDesignFile(CreateNewProject.GetDBName());
                ViewManager.FitView();
            }  
        
        }


    }
}
